#include "networkessential.h"

bool sendData(SOCKET s, const char *buf, int size)
{
	int sendedsize = 0;

	while (sendedsize < size)
	{
		int sendingsize = send(s, buf + sendedsize, size - sendedsize, 0);
		if (sendingsize < 0)
		{
			return false;
		}
		sendedsize += sendingsize;
	}
	return true;
}

bool sendDataTo(SOCKET s, const char *buf, int size, sockaddr *toAddress, socklen_t length)
{
	int sendedsize = 0;

	while (sendedsize < size)
	{
		int sendingsize = sendto(s, buf + sendedsize, size - sendedsize, 0, toAddress, length);
		if (sendedsize < 0)
		{
			return false;
		}
		sendedsize += sendingsize;
	}

	return true;
}

bool recvData(SOCKET s, char *buf, int size)
{
	int recvedsize = 0;

	while (recvedsize < size)
	{
		int recvingsize = recv(s, buf + recvedsize, size - recvedsize, 0);
		if (recvingsize < 0)
		{
			return false;
		}
		recvedsize += recvingsize;
	}

	return true;
}

bool recvDataFrom(SOCKET s, char *buf, int size, sockaddr *fromAddress, socklen_t length)
{
	int recvedsize = 0;

	while (recvedsize < size)
	{
		int recvingsize = recvfrom(s, buf + recvedsize, size - recvedsize, 0, fromAddress, &length);
		if (recvingsize < 0)
		{
			return false;
		}
		recvedsize += recvingsize;
	}

	return true;
}
